<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Class Mcontrol extends MY_Model
{
    public function __construct()
    {
        parent::__construct();
    }
    
    public function getTvshowCategories()
    {
		$sql = "SELECT * FROM `tvshow` WHERE `active` = 1 ORDER BY id DESC";    
        $count = $this->db->query($sql)->num_rows();
        $xs = $this->db->query($sql)->result_array();
        //return array('data' => $xs, 'count' => $count);
        return $xs;
    }
    
    public function getVoteListTvShow($id)
    {
        return $this->db->query('SELECT c.id AS cid, q.id AS qid, q.content, q.tvshow_id, q.created, q.active, q.isshare FROM questions AS q INNER JOIN tvshow AS ts ON q.tvshow_id = ts.id INNER JOIN channel AS c ON ts.channel_id = c.id WHERE q.tvshow_id = '.$id.' AND q.active = 0 ORDER BY q.id DESC')->result_object();
    }
    
    public function getVoteListTvShowIsStart($id)
    {
        return $this->db->query('SELECT c.id AS cid, q.id AS qid, q.content, q.tvshow_id, q.created, q.active, q.isshare FROM questions AS q INNER JOIN tvshow AS ts ON q.tvshow_id = ts.id INNER JOIN channel AS c ON ts.channel_id = c.id WHERE q.tvshow_id = '.$id.' AND q.active = 1 ORDER BY q.id DESC')->result_object();
    }
    
    public function getIDChannelTemplate($id)
    {
        return $this->db->query('SELECT c.id AS cid, c.logo, ts.`name`, ts.background, ts.id AS tsid, ts.`starting` FROM tvshow AS ts INNER JOIN channel AS c ON ts.channel_id = c.id WHERE ts.id = '.$id)->row_object();
    }
    
    public function UpdateTvShowDB($id,$tvshow_name,$channel_id)
    {
        $active = $this->db->query('SELECT `starting` FROM tvshow WHERE id = '.$id)->row_object()->starting;
        $set = 0;
        if($active == 0)
        {
            $set = 1;
        }
        $this->db->update('tvshow', array('starting' => $set), "id = ".$id);
        
        if($set == 1)
        {
            $data = array(
                'tvshow_id' => $id,
                'tvshow_name' => $tvshow_name,
                'channel_id' => $channel_id,
                'time_start' => strtotime(date('Y-m-d H:i:s'))
            );
            $this->db->insert('tvshow_log', $data);
            $id_log = $this->db->insert_id();
            return json_encode(array('set' => (string)$set, 'id_log' => $id_log));
        }else{
            return json_encode(array('set' => (string)$set));
        }        
    }
    
    public function StopTvShowDB($id_log)
    {
        $this->db->update('tvshow_log', array('time_end' => strtotime(date('Y-m-d H:i:s'))), "id = ".$id_log);
    }
    
    public function addThisQuestion($tvshow_id,$question,$choice1,$choice2,$choice3,$choice4)
    {
        $q = array('question' => $question);
        $x = array('choice' => $as = array(
            'id_1' => array("1",$choice1),
            'id_2' => array("2",$choice2),
            'id_3' => array("3",$choice3),
            'id_4' => array("4",$choice4),
        ));
        $arr = json_encode(array_merge($q, $x));
        
        $data = array(
            'tvshow_id' => $tvshow_id,
            'content' => $arr
            
        );
        $this->db->insert('questions', $data);
        return 200;
    }
    
    public function ajaxFilterTvshow($keyword){
        if( $keyword =="" || $keyword == null ){
            $sql = "SELECT * FROM `tvshow` WHERE `active` = 1 ORDER BY id DESC";
        }else{
            $sql = "SELECT * FROM `tvshow` WHERE name LIKE '%".$keyword."%' AND `active` = 1 ORDER BY id DESC";
        }
        $xs = $this->db->query($sql)->result_object();
        return $xs;
    }
    
    public function ajaxGetTvshow($tvshow_id){
        $sql = "SELECT * FROM `tvshow` WHERE id = '$tvshow_id' AND `active` = 1 ";
        $xs = $this->db->query($sql)->result_object();
        return $xs;
    }
    
    
    public function UpdateStatusQuestionDB($id, $status)
    {
        $this->db->update('questions', array('active' => $status), "id = ".$id);
        return 200;
    }
    
    public function questionHistoriesDb($id)
    {
        $sql = "SELECT q.content FROM questions AS q LEFT JOIN tvshow_log AS tl ON q.id_log = tl.id WHERE tl.id = '".$id."' AND q.created BETWEEN tl.time_start AND tl.time_end ORDER BY q.id DESC";
        $ques = $this->db->query($sql)->result_object();
        
        $data = $this->db->query("SELECT * FROM tvshow_log WHERE id = ".$id)->result_object();
        return array('question' => $ques, 'data' => $data[0]);
    }
    public function showHistoriesDb()
    {
        $sql = "SELECT tl.*,ch.name AS channel FROM tvshow_log AS tl LEFT JOIN channel AS ch ON tl.channel_id = ch.id ORDER BY tl.id DESC";
        $data = $this->db->query($sql)->result_object();
        return $data;
    }
}
